package com.note.chh.notebook.UI.homeFragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.note.chh.notebook.Adapter.StrokeAdapter;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.Model.Stroke;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseFragment;
import com.note.chh.notebook.UI.stroke.AddStrokeActivity;
import com.note.chh.notebook.UI.stroke.CheckStrokeActivity;
import com.note.chh.notebook.Uitls.EventReminderUtils;
import com.note.chh.notebook.Uitls.ItemDivider;
import com.note.chh.notebook.Uitls.StrokeUtilsDao;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 行程 fragment
 */
public class StrokeFragment extends BaseFragment {


    @BindView(R.id.mRv)
    RecyclerView mRv;
    @BindView(R.id.mImage_addNote)
    ImageView mImageAddNote;
    @BindView(R.id.mText_empty)
    TextView mTextEmpty;
    private BaseQuickAdapter strokeAdapter;

    public StrokeFragment() {
        // Required empty public constructor
    }


    @Override
    public int bindLayout() {
        return R.layout.fragment_stroke;
    }

    @Override
    public void initView(View view) {
        mRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRv.setHasFixedSize(true);
        mRv.addItemDecoration(new ItemDivider().setDividerWith(SizeUtils.dp2px(10)).setDividerColor(getResources().getColor(R.color.whiteBg)));
        strokeAdapter = new StrokeAdapter(R.layout.item_stroke);
        strokeAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRv.setAdapter(strokeAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        List<Stroke> strokes = StrokeUtilsDao.queryAll();
        for (int i = 0; i < strokes.size(); i++) {
            Stroke stroke = strokes.get(i);
            long l1 = TimeUtils.string2Millis(stroke.getStartTime() + ":00");
            long l2 = TimeUtils.string2Millis(TimeUtils.getNowString());
            if (l1<l2) {
                StrokeUtilsDao.deleteStroke(stroke.getId());
                EventReminderUtils.deleteCalendarEvent(getActivity(),stroke.getTitle());
            }
        }
        upStrokeList();
    }

    @Override
    public void initData() {

//        upStrokeList();
    }

    @Override
    public void initListener() {
        super.initListener();
        strokeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Stroke stroke = (Stroke) adapter.getData().get(position);
                Bundle bundle = new Bundle();
                bundle.putLong("id",stroke.getId());
                startActivity(CheckStrokeActivity.class,bundle);
            }
        });
    }

    @OnClick(R.id.mImage_addNote)
    public void onViewClicked() {
        startActivityForResult(AddStrokeActivity.class, Constant.ADD_STROKE_REQUESTCODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ADD_STROKE_REQUESTCODE && resultCode == Constant.ADD_STROKE_RESULTCODE) {
            upStrokeList();
        }
    }

    private void upStrokeList(){
        List<Stroke> strokes = StrokeUtilsDao.queryAll();
        if (strokes.size() <= 0) {
            mTextEmpty.setVisibility(View.VISIBLE);
        } else {
            Collections.reverse(strokes);
            mTextEmpty.setVisibility(View.GONE);
        }
        strokeAdapter.setNewData(strokes);
    }

}
